﻿<?xml version="1.0" encoding="utf-8"?><Articles><Article><ArticlesID>844</ArticlesID><Title>Giảm k&amp;#237;ch thước file CSS với .NET</Title><Body>&amp;lt;div class=&amp;quot;BlogPostExcerptNews&amp;quot;&amp;gt;Giảm khoảng 5% k&amp;amp;iacute;ch thước CSS sẽ l&amp;amp;agrave;m cho tốc độ load CSS  trong trang web giảm đ&amp;amp;aacute;ng kể. Sử dụng đoạn m&amp;amp;atilde; sau đ&amp;amp;acirc;y gi&amp;amp;uacute;p bạn thực hiện  điều n&amp;amp;agrave;y bằng c&amp;amp;aacute;ch loại bỏ bớt c&amp;amp;aacute;c khoảng trắng trong t&amp;amp;agrave;i liệu CSS để  tạo ra file mới với k&amp;amp;iacute;ch thước nhỏ hơn.&amp;lt;/div&amp;gt;
&amp;lt;pre title=&amp;quot;code&amp;quot; class=&amp;quot;brush: csharp;&amp;quot;&amp;gt;
public static string RemoveWhiteSpaceFromStylesheets(string body)
{
    body = Regex.Replace(body, @&amp;amp;quot;[a-zA-Z]+#&amp;amp;quot;, &amp;amp;quot;#&amp;amp;quot;); // ul#
    body = Regex.Replace(body, @&amp;amp;quot;[\n\r]+\s*&amp;amp;quot;, string.Empty); // space
    body = Regex.Replace(body, @&amp;amp;quot;\s+&amp;amp;quot;, &amp;amp;quot; &amp;amp;quot;);
    body = Regex.Replace(body, @&amp;amp;quot;\s?([:,;{}])\s?&amp;amp;quot;, &amp;amp;quot;$1&amp;amp;quot;);
    body = body.Replace(&amp;amp;quot;;}&amp;amp;quot;, &amp;amp;quot;}&amp;amp;quot;);
    body = Regex.Replace(body, @&amp;amp;quot;([\s:]0)(px|pt|%|em)&amp;amp;quot;, &amp;amp;quot;$1&amp;amp;quot;);
    // Remove comments from CSS
    body = Regex.Replace(body, @&amp;amp;quot;/\*[\d\D]*?\*/&amp;amp;quot;, string.Empty);
    return body;
}
static void Main(string[] args)
{
    string minstyle = RemoveWhiteSpaceFromStylesheets(File.ReadAllText(@&amp;amp;quot;..\..\style.css&amp;amp;quot;));
    File.WriteAllText(@&amp;amp;quot;..\..\style-min.css&amp;amp;quot;, minstyle);
    Console.WriteLine(&amp;amp;quot;Done!&amp;amp;quot;);
    Console.Read();
}
&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;Code VB.NET&amp;lt;/p&amp;gt;
&amp;lt;pre title=&amp;quot;code&amp;quot; class=&amp;quot;brush: vb;&amp;quot;&amp;gt;
Imports System.IO
Imports System.Text.RegularExpressions
 
Module MinCSS
 
    Sub Main()
        Dim minstyle As String = RemoveWhiteSpaceFromStylesheets(File.ReadAllText(&amp;amp;quot;..\..\style.css&amp;amp;quot;))
        File.WriteAllText(&amp;amp;quot;..\..\style-min.css&amp;amp;quot;, minstyle)
        Console.Write(&amp;amp;quot;Done!&amp;amp;quot;)
        Console.Read()
    End Sub
 
    Public Function RemoveWhiteSpaceFromStylesheets(ByVal body As String) As String
        body = Regex.Replace(body, &amp;amp;quot;[a-zA-Z]+#&amp;amp;quot;, &amp;amp;quot;#&amp;amp;quot;)
        body = Regex.Replace(body, &amp;amp;quot;[\n\r]+\s*&amp;amp;quot;, String.Empty)
        body = Regex.Replace(body, &amp;amp;quot;\s+&amp;amp;quot;, &amp;amp;quot; &amp;amp;quot;)
        body = Regex.Replace(body, &amp;amp;quot;\s?([:,;{}])\s?&amp;amp;quot;, &amp;amp;quot;$1&amp;amp;quot;)
        body = body.Replace(&amp;amp;quot;;}&amp;amp;quot;, &amp;amp;quot;}&amp;amp;quot;)
        body = Regex.Replace(body, &amp;amp;quot;([\s:]0)(px|pt|%|em)&amp;amp;quot;, &amp;amp;quot;$1&amp;amp;quot;)
        ' Remove comments from CSS
        body = Regex.Replace(body, &amp;amp;quot;/\*[\d\D]*?\*/&amp;amp;quot;, String.Empty)
        Return body
    End Function
 
End Module
&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;Press F5 ch&amp;amp;uacute;ng ta sẽ c&amp;amp;oacute; 1 file style-min.css với k&amp;amp;iacute;ch thước nhỏ hơn  so với file gốc.&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;a href=&amp;quot;http://code2code.googlecode.com/svn/trunk/MinimizeCSS&amp;quot;&amp;gt;http://code2code.googlecode.com/svn/trunk/MinimizeCSS&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;Reference : &amp;lt;a title=&amp;quot;http://code2code.info/file.axd?file=2010%2f3%2fMinimizeCSS.rar&amp;quot; href=&amp;quot;http://dev.meotom.net/Uploads/admin/2010/3/17/MinimizeCSS.rar&amp;quot;&amp;gt;MinimizeCSS.rar&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;&amp;amp;nbsp;&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;&amp;amp;nbsp;&amp;lt;/p&amp;gt;</Body><CommentEnabled>True</CommentEnabled><ViewCount>0</ViewCount><ReleaseDate>3/18/2010 8:45:53 AM</ReleaseDate><TotalComment>0</TotalComment><IsMedia>False</IsMedia><IsPicture>False</IsPicture><IsVote>True</IsVote><ArticlePassword /><PostVisible>0</PostVisible><ArticlePath>/2010/3/17/Giam-kich-thuoc-file-CSS-voi-NET-34C</ArticlePath><Approved>True</Approved><ImagePath /><CategoryId>28</CategoryId><ArrayCatID>28|25</ArrayCatID></Article></Articles>